﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
using System.Data.SqlClient;
using log4net;
using System.Globalization;

namespace Tireguit.Nmon.Parser.Statisitic
{
    public class MEMUSEElement : NmonStatElement
    {
        protected new ILog _log = LogManager.GetLogger(typeof(MEMUSEElement));
        protected IDataParameter _zzzz = null;
        protected IDataParameter _numperm = null;
        protected IDataParameter _minperm = null;
        protected IDataParameter _maxperm = null;
        protected IDataParameter _minfree = null;
        protected IDataParameter _maxfree = null;
        public string Zzzz { get; protected set; }
        public float NumPerm { get; protected set; }
        public float MinPerm { get; protected set; }
        public float MaxPerm { get; protected set; }
        public int MinFree { get; protected set; }
        public int MaxFree { get; protected set; }
        public MEMUSEElement(IDbConnection connection)
        {
            Balise = BaliseType.MEMUSE;
            ListCommand = new List<System.Data.IDbCommand>();
            Connection = connection;
            Linestring = string.Empty;
            _insertreq = "INSERT INTO " + NmonTables.TablesDictionnary.Instance["MEMUSE"] + " ([zzzz],[numperm],[minperm],[maxperm],[minfree],[maxfree]) VALUES (@zzzz,@numperm,@minperm,@maxperm,@minfree,@maxfree)";
            Command = Connection.CreateCommand();
            Command.CommandText = _insertreq;
            ListCommand.Add(Command);
            _zzzz = Command.CreateParameter();
            _zzzz.ParameterName = "@zzzz";
            _zzzz.DbType = DbType.String;
            _numperm = Command.CreateParameter();
            _numperm.ParameterName = "@numperm";
            _numperm.DbType = DbType.Decimal;
            _minperm = Command.CreateParameter();
            _minperm.ParameterName = "@minperm";
            _minperm.DbType = DbType.Decimal;
            _maxperm = Command.CreateParameter();
            _maxperm.ParameterName = "@maxperm";
            _maxperm.DbType = DbType.Decimal;
            _minfree = Command.CreateParameter();
            _minfree.ParameterName = "@minfree";
            _minfree.DbType = DbType.Int32;
            _maxfree = Command.CreateParameter();
            _maxfree.ParameterName = "@maxfree";
            _maxfree.DbType = DbType.Int32;
        }
        public MEMUSEElement() : this(null) { }
        public override List<IDbCommand> GetInsertReq()
        {
            try
            {
                _log.Debug("start GetInsertReq");


                if (Command == null)
                {
                    Command = Connection.CreateCommand();
                    Command.CommandText = _insertreq;
                    ListCommand.Add(Command);
                }
                Command.Parameters.Clear();
                _zzzz.Value = Zzzz;
                _numperm.Value = NumPerm;
                _minperm.Value = MinPerm;
                _maxperm.Value = MaxPerm;
                _minfree.Value = MinFree;
                _maxfree.Value = MaxFree;
                Command.Parameters.Add(_zzzz);
                Command.Parameters.Add(_numperm);
                Command.Parameters.Add(_minperm);
                Command.Parameters.Add(_maxperm);
                Command.Parameters.Add(_minfree);
                Command.Parameters.Add(_maxfree);

                return ListCommand;

            }
            catch (System.Exception ex)
            {
                _log.Error("GetInsertReq", ex);
                return null;
            }
            finally
            {
                _log.Debug("stop GetInsertReq");
            }
        }
        protected override bool InternalParse(string line)
        {
            try
            {
                _log.Debug("start InternalParse");
                if (string.IsNullOrEmpty(line))
                {
                    return false;
                }

                // AAAElement aaaelem = new AAAElement();
                string[] elements = line.Split(separ);
#if DEBUG
                if (!elements[0].StartsWith("MEMUSE"))
                {
                    _log.Error("Not MEMUSE Line");
                    throw new ArgumentOutOfRangeException("start");
                }
#endif
                if (elements[1].StartsWith("Mem"))
                {
                    //Skip information header 
                    return false;
                }
                Zzzz = elements[1];
                float res = -1;
                if (float.TryParse(elements[2], NumberStyles.Float, CultureInfo.InvariantCulture, out res))
                {
                    NumPerm = res;

                }
                if (float.TryParse(elements[3], NumberStyles.Float, CultureInfo.InvariantCulture, out res))
                {
                    MinPerm = res;

                }
                if (float.TryParse(elements[4], NumberStyles.Float, CultureInfo.InvariantCulture, out res))
                {
                    MaxPerm = res;

                }
                int res2 = -1;
                if (int.TryParse(elements[5], NumberStyles.Float, CultureInfo.InvariantCulture, out res2))
                {
                    MinFree = res2;

                }
                if (int.TryParse(elements[6], NumberStyles.Float, CultureInfo.InvariantCulture, out res2))
                {
                    MaxFree = res2;

                }


                return true;
                //  return aaaelem;

            }
            catch (System.Exception ex)
            {
                _log.Error("InternalParse", ex);
                return false;
            }
            finally
            {
                _log.Debug("stop InternalParse");
            }
        }
    }
}
